package MapBase.logic.decorate.floor
{
	import MapBase.logic.decorate.tile.EntityFloorTile;
	
	import com.friendsofed.isometric.IsoUtils;
	import com.friendsofed.isometric.Point3D;
	import com.friendsofed.isometric.PointExchange;
	import com.utils.astar.Grid;
	
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class GridFloor
	{
		private var _obj:Sprite;
		
		private var _grid : Grid;
		
		public var tileList : Object;
		
		public var color:uint = 0x00FF00;
		
		private var _tileWidth : Number;
		private var _tileHeight : Number;
		private var _type : int ;
		
		public function GridFloor(obj:Sprite,grid:Grid,tileWidth:Number,tileHeight:Number,type:int=1)
		{
			_obj = obj;
			_grid = grid;
			_tileWidth = tileWidth;
			_tileHeight = tileHeight;
			_type = type;
			
			draw(type);
		}
		
		public function invalidate():void
		{
			draw(_type);
		}
		
		/**
		 * 
		 * @param type 
		 * 
		 */		
		public function draw(type:int = 1):void
		{
			var colNum : int = _grid.numCols;
			var rowNum : int = _grid.numRows;
			
			tileList = new Object;
			
			for(var c:int =0;c<colNum;c++)
			{
				for(var r:int=0;r<rowNum;r++)
				{
					var tile:Sprite = new Sprite;
					
					var color : uint;
					if(_grid.getNode(c,r).walkable)
					{
						color = EntityFloorTile.COLOR_NORMAL;
					}
					else
					{
						color = EntityFloorTile.COLOR_BLOCK;
					}
					
					new EntityFloorTile(tile,_tileWidth,_tileHeight,0x00FF00,type);
					
					var point : Point ;
					
					if(type == 1)
					{
						point = IsoUtils.isoToScreen(new Point3D(c*_tileHeight,0,r*_tileHeight));
					}else if(type == 2)
					{
						point = PointExchange.LogicToStage(new Point(c,r));
					}else if(type == 3)
					{
						point = new Point(c*_tileHeight,r*_tileHeight);
					}
					
					tile.x=point.x;
					tile.y=point.y;
					_obj.addChild(tile);
					
					var id:String = c+","+r;
					tileList[id] = new Object;
					tileList[id]["tile"] = tile;
					tileList[id]["statu"] = 0;
				}
			}
		}
	}
}